package org.example.BinaryTree;

import java.util.Stack;

public class Sum_of_Left_Leaves {
    public static int sum = 0;
    public static void main(String[] args) {
        //左叶子之和

        //给定二叉树的根节点 root ，返回所有左叶子之和。
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode root = new TreeNode(0);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        node5.left = node7;
        get(root);
        System.out.println(sum);
    }
    public static void get(TreeNode root) {
        if (root==null) return;
        get(root.left);
        get(root.right);
        if (root.left!=null && root.left.left==null && root.left.right==null) sum+= root.left.val;
    }
}
